package com.ashlikun.wheelview_rv

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager

/**
 * @author　　: 李坤
 * 创建时间: 2024/7/23 13:45
 * 邮箱　　：496546144@qq.com
 *
 * 功能介绍：
 */
internal object WheelUtils {
    /**
     * 根据item的大小(弧的长度),和item对应的旋转角度,计算出滑轮轴的半径
     */
    fun radianToRadio(radian: Int, degree: Float) = radian * 180.0 / (degree * Math.PI)

    /**
     * 根据方向代码创建view layoutparams
     *
     * 如果水平布局时,最好指定高度大小,  垂直布局时最定宽度大小
     */
    fun createLayoutParams(orientation: Int, size: Int): ViewGroup.LayoutParams {
        return if (orientation == RecyclerView.VERTICAL) {
            ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, size)
        } else {
            ViewGroup.LayoutParams(size, ViewGroup.LayoutParams.MATCH_PARENT)
        }
    }

    /**
     * 获取控件的中心x位置
     */
    fun getViewCenterX(view: View): Int {
        return view.left + view.right shr 1
    }

    /**
     * 获取控件的中心y位置
     */
    fun getViewCenterY(view: View): Int {
        return view.top + view.bottom shr 1
    }

    /**
     * 获取循环状态下的真是位置
     */
    fun getRealPosition(position: Int, allNumber: Int, offsetCount: Int): Int {
        if (allNumber <= offsetCount * 2 + 1) return 0
        if (position < offsetCount) {
            val diff: Int = offsetCount - position
            //最后一个
            return allNumber - offsetCount * 2 - diff
        } else if (position >= allNumber - offsetCount) {
            //第一个
            return position - (allNumber - offsetCount)
        }
        return position - offsetCount
    }

    /**
     * 查找屏幕中第一个item位置
     *
     * @param layoutManager
     * @return
     */
    fun findFirstVisibleItemPosition(layoutManager: RecyclerView.LayoutManager): Int {
        var firstVisibleItem = -1
        if (layoutManager is GridLayoutManager) {
            firstVisibleItem = layoutManager.findFirstVisibleItemPosition()
        } else if (layoutManager is LinearLayoutManager) {
            firstVisibleItem = layoutManager.findFirstVisibleItemPosition()
        } else if (layoutManager is StaggeredGridLayoutManager) {
            val firstPositions = IntArray(layoutManager.spanCount)
            layoutManager.findFirstVisibleItemPositions(firstPositions)
            firstVisibleItem = findMaxOrMin(firstPositions, false)
        }
        return firstVisibleItem
    }

    /**
     * 查找屏幕中最后一个item位置
     */
    fun findLastVisiblePosition(layoutManager: RecyclerView.LayoutManager): Int {
        val lastVisibleItemPosition: Int
        lastVisibleItemPosition = if (layoutManager is GridLayoutManager) {
            layoutManager.findLastVisibleItemPosition()
        } else if (layoutManager is StaggeredGridLayoutManager) {
            val into = layoutManager.findLastVisibleItemPositions(null)
            findMaxOrMin(into, true)
        } else {
            (layoutManager as LinearLayoutManager).findLastVisibleItemPosition()
        }
        return lastVisibleItemPosition
    }

    private fun findMaxOrMin(lastPositions: IntArray, isMax: Boolean): Int {
        var max = lastPositions[0]
        for (value in lastPositions) {
            if (isMax) {
                if (value > max) {
                    max = value
                }
            } else {
                if (value < max) {
                    max = value
                }
            }
        }
        return max
    }

    fun dip2px(context: Context, dipValue: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (dipValue * scale + 0.5f).toInt()
    }
}
